# Replication Code for Descriptive and Regression Analysis "International climate litigation unlikely to create backlash among Swiss voters" #####################################################
# Authors: Lukas Fesenfeld, Laurie Durel, Leon Sistek
# Date: 27.08.2025

# R.version
# platform       aarch64-apple-darwin20     
# arch           aarch64                    
# os             darwin20                   
# system         aarch64, darwin20          
# status                                    
# major          4                          
# minor          3.2                        
# year           2023                       
# month          10                         
# day            31                         
# svn rev        85441                      
# language       R                          
# version.string R version 4.3.2 (2023-10-31)
# nickname       Eye Holes   


setwd("[Insert Working Directory]")

# Packages #################################
library(tidyverse)
library(sandwich)
library(lmtest)
library(estimatr)
library(modelsummary)
library(jtools)
library(broom)
library(broom.mixed)
library(ggplot2)
library(ggpubr)
library(dplyr)
library(emmeans)
library(patchwork)
library(kableExtra)
library(htmltools)
library(texreg)
library(stargazer)

# Import Data ####################################################
filename <- "data/data_cleaned.csv"
data <- read.csv(filename, header = TRUE, sep = ",")

# Subset the data ########################################
#Exclude those who already voted by mail or are not entitled to vote
data_vote <- data[data$take_part != 99 & data$take_part != 100, ]

#Additionally exclude those who did not actually vote.
#data_vote2 <- data[data$take_part != 99 & data$take_part != 100, ]
data_vote_fin2 <- data[data$take_part != 99 &
                       data$take_part != 100 &
                       data$take_part_pv != 100 &
                       !is.na(data$vote_binary_pv), ]


# Descriptives #########################################

## Wave 1 ##############################################
### SI Table 1 ##########################################


response_labels <- c("0" = "Unimportant", "1" = "Important")

var <- "environment"

# Create table
tab <- table(data_vote[[var]])
prop <- prop.table(tab)

# Build labeled tibble
table_1 <- tibble(
  Variable = "Importance of Climate and Environmental Protection",
  Response = response_labels[names(prop)],
  Proportion = round(as.numeric(prop) * 100, 1),
  N = sum(tab[names(tab) %in% names(response_labels)])
) %>%
  filter(Response %in% response_labels) %>%
  pivot_wider(names_from = Response, values_from = Proportion, values_fill = 0) %>%
  relocate(N, .after = last_col())

# Add % signs
cols_to_format <- setdiff(names(table_1), c("Variable", "N"))

table_1 <- table_1 %>%
  mutate(across(all_of(cols_to_format), ~ paste0(., "%")))

table_latex_1 <- table_1 %>%
  kbl(digits = 1, format = "latex", booktabs = TRUE, align = c('lccr')) %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10) %>%
  column_spec(1, width = "12em") %>%
  column_spec(2, width = "8em") %>%
  column_spec(3, width = "8em")

save_kable(table_latex_1, file = "tables/SI_Table_1.tex")

### SI Table 2 ##########################################

#Q83
response_labels <- c("1" = "Yes, I know this case and the court decision",
                     "2" = "I have already heard about the case, but do not know the court decision",
                     "3" = "No, never heard of it")

var <- "Q83"

# Create table
tab <- table(data_vote[[var]])
prop <- prop.table(tab)

# Build labeled tibble
table_4 <- tibble(
  Variable = "Knowledge of case",
  Response = response_labels[names(prop)],
  Proportion = round(as.numeric(prop) * 100, 1),
  N = sum(tab[names(tab) %in% names(response_labels)])
) %>%
  filter(Response %in% response_labels) %>%
  pivot_wider(names_from = Response, values_from = Proportion, values_fill = 0) %>%
  relocate(N, .after = last_col())

# Add % signs
cols_to_format <- setdiff(names(table_4), c("Variable", "N"))

table_4 <- table_4 %>%
  mutate(across(all_of(cols_to_format), ~ paste0(., "%")))

table_latex_4 <- table_4 %>%
  kbl(format = "latex", digits = 1, booktabs = TRUE, align = c('lcccr')) %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10) %>%
  column_spec(1, width = "10em") %>%
  column_spec(2, width = "15em") %>%
  column_spec(3, width = "15em") %>%
  column_spec(4, width = "8em")

save_kable(table_latex_4, file = "tables/SI_Table_2.tex")





### SI Table 3 ##########################################
#Q226 (Support)
response_labels <- c("1" = "Completely reject",
                     "2" = "Reject",
                     "3" = "Rather reject",
                     "4" = "Neither nor",
                     "5" = "Rather support",
                     "6" = "Support",
                     "7" = "Completely support")

var <- "Q226"

# Create table
tab <- table(data_vote[[var]])
prop <- prop.table(tab)

# Build labeled tibble
table_3 <- tibble(
  Variable = "Support court",
  Response = response_labels[names(prop)],
  Proportion = round(as.numeric(prop)*100, 1),
  N = sum(tab[names(tab) %in% names(response_labels)])
) %>%
  filter(Response %in% response_labels) %>%
  pivot_wider(names_from = Response, values_from = Proportion, values_fill = 0) %>%
  relocate(N, .after = last_col())

# Add % signs
cols_to_format <- setdiff(names(table_3), c("Variable", "N"))

table_3 <- table_3 %>%
  mutate(across(all_of(cols_to_format), ~ paste0(., "%")))

table_latex_3 <- table_3 %>%
  kbl(format = "latex", digits = 1, booktabs = TRUE, align = c('lcccccccr')) %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10) %>%
  column_spec(1, width = "9em") %>%
  column_spec(2, width = "7em") %>%
  column_spec(3, width = "7em") %>%
  column_spec(4, width = "7em") %>%
  column_spec(5, width = "7em") %>%
  column_spec(6, width = "7em") %>%
  column_spec(7, width = "7em") %>%
  column_spec(8, width = "7em")

save_kable(table_latex_3, file = "tables/SI_Table_3.tex")
### SI Table 4 ##########################################


response_labels <- c("1" = "Do not trust at all",
                     "2" = "Do not trust",
                     "3" = "Rather not trust",
                     "4" = "Neither nor",
                     "5" = "Trust somewhat",
                     "6" = "Trust",
                     "7" = "Trust fully")

var <- "Q84"

# Create table
tab <- table(data_vote[[var]])
prop <- prop.table(tab)

# Build labeled tibble
table_2 <- tibble(
  Variable = "Trust judges",
  Response = response_labels[names(prop)],
  Proportion = round(as.numeric(prop)*100, 1),
  N = sum(tab[names(tab) %in% names(response_labels)])
) %>%
  filter(Response %in% response_labels) %>%
  pivot_wider(names_from = Response, values_from = Proportion, values_fill = 0) %>%
  relocate(N, .after = last_col())

# Add % signs
cols_to_format <- setdiff(names(table_2), c("Variable", "N"))

table_2 <- table_2 %>%
  mutate(across(all_of(cols_to_format), ~ paste0(., "%")))

table_latex_2 <- table_2 %>%
  kbl(format = "latex", digits = 1, booktabs = TRUE, align = c('lcccccccr')) %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10) %>%
  column_spec(1, width = "8em") %>%
  column_spec(2, width = "7em") %>%
  column_spec(3, width = "7em") %>%
  column_spec(4, width = "7em") %>%
  column_spec(5, width = "7em") %>%
  column_spec(6, width = "7em") %>%
  column_spec(7, width = "7em") %>%
  column_spec(8, width = "7em")

save_kable(table_latex_2, file = "tables/SI_Table_4.tex")

## Wave 2 ##############################################
### SI Table 5 ##########################################


response_labels <- c("0" = "Unimportant", "1" = "Important")

var <- "environment"

# Create table
tab <- table(data_vote_fin2[[var]])
prop <- prop.table(tab)

# Build labeled tibble
table_1 <- tibble(
  Variable = "Importance of Climate and Environmental Protection",
  Response = response_labels[names(prop)],
  Proportion = round(as.numeric(prop) * 100, 1),
  N = sum(tab[names(tab) %in% names(response_labels)])
) %>%
  filter(Response %in% response_labels) %>%
  pivot_wider(names_from = Response, values_from = Proportion, values_fill = 0) %>%
  relocate(N, .after = last_col())

# Add % signs
cols_to_format <- setdiff(names(table_1), c("Variable", "N"))

table_1 <- table_1 %>%
  mutate(across(all_of(cols_to_format), ~ paste0(., "%")))

table_latex_1 <- table_1 %>%
  kbl(digits = 1, format = "latex", booktabs = TRUE, align = c('lccr')) %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10) %>%
  column_spec(1, width = "12em") %>%
  column_spec(2, width = "8em") %>%
  column_spec(3, width = "8em")

save_kable(table_latex_1, file = "tables/SI_Table_5.tex")

### SI Table 6 ##########################################

#Q83
response_labels <- c("1" = "Yes, I know this case and the court decision",
                     "2" = "I have already heard about the case, but do not know the court decision",
                     "3" = "No, never heard of it")

var <- "Q83"

# Create table
tab <- table(data_vote_fin2[[var]])
prop <- prop.table(tab)

# Build labeled tibble
table_4 <- tibble(
  Variable = "Knowledge of case",
  Response = response_labels[names(prop)],
  Proportion = round(as.numeric(prop) * 100, 1),
  N = sum(tab[names(tab) %in% names(response_labels)])
) %>%
  filter(Response %in% response_labels) %>%
  pivot_wider(names_from = Response, values_from = Proportion, values_fill = 0) %>%
  relocate(N, .after = last_col())

# Add % signs
cols_to_format <- setdiff(names(table_4), c("Variable", "N"))

table_4 <- table_4 %>%
  mutate(across(all_of(cols_to_format), ~ paste0(., "%")))

table_latex_4 <- table_4 %>%
  kbl(format = "latex", digits = 1, booktabs = TRUE, align = c('lcccr')) %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10) %>%
  column_spec(1, width = "10em") %>%
  column_spec(2, width = "15em") %>%
  column_spec(3, width = "15em") %>%
  column_spec(4, width = "8em")

save_kable(table_latex_4, file = "tables/SI_Table_6.tex")





### SI Table 7 ##########################################
#Q226 (Support)
response_labels <- c("1" = "Completely reject",
                     "2" = "Reject",
                     "3" = "Rather reject",
                     "4" = "Neither nor",
                     "5" = "Rather support",
                     "6" = "Support",
                     "7" = "Completely support")

var <- "Q226"

# Create table
tab <- table(data_vote_fin2[[var]])
prop <- prop.table(tab)

# Build labeled tibble
table_3 <- tibble(
  Variable = "Support court",
  Response = response_labels[names(prop)],
  Proportion = round(as.numeric(prop)*100, 1),
  N = sum(tab[names(tab) %in% names(response_labels)])
) %>%
  filter(Response %in% response_labels) %>%
  pivot_wider(names_from = Response, values_from = Proportion, values_fill = 0) %>%
  relocate(N, .after = last_col())

# Add % signs
cols_to_format <- setdiff(names(table_3), c("Variable", "N"))

table_3 <- table_3 %>%
  mutate(across(all_of(cols_to_format), ~ paste0(., "%")))

table_latex_3 <- table_3 %>%
  kbl(format = "latex", digits = 1, booktabs = TRUE, align = c('lcccccccr')) %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10) %>%
  column_spec(1, width = "9em") %>%
  column_spec(2, width = "7em") %>%
  column_spec(3, width = "7em") %>%
  column_spec(4, width = "7em") %>%
  column_spec(5, width = "7em") %>%
  column_spec(6, width = "7em") %>%
  column_spec(7, width = "7em") %>%
  column_spec(8, width = "7em")

save_kable(table_latex_3, file = "tables/SI_Table_7.tex")
### SI Table 8 ##########################################


response_labels <- c("1" = "Do not trust at all",
                     "2" = "Do not trust",
                     "3" = "Rather not trust",
                     "4" = "Neither nor",
                     "5" = "Trust somewhat",
                     "6" = "Trust",
                     "7" = "Trust fully")

var <- "Q84"

# Create table
tab <- table(data_vote_fin2[[var]])
prop <- prop.table(tab)

# Build labeled tibble
table_2 <- tibble(
  Variable = "Trust judges",
  Response = response_labels[names(prop)],
  Proportion = round(as.numeric(prop)*100, 1),
  N = sum(tab[names(tab) %in% names(response_labels)])
) %>%
  filter(Response %in% response_labels) %>%
  pivot_wider(names_from = Response, values_from = Proportion, values_fill = 0) %>%
  relocate(N, .after = last_col())

# Add % signs
cols_to_format <- setdiff(names(table_2), c("Variable", "N"))

table_2 <- table_2 %>%
  mutate(across(all_of(cols_to_format), ~ paste0(., "%")))

table_latex_2 <- table_2 %>%
  kbl(format = "latex", digits = 1, booktabs = TRUE, align = c('lcccccccr')) %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10) %>%
  column_spec(1, width = "8em") %>%
  column_spec(2, width = "7em") %>%
  column_spec(3, width = "7em") %>%
  column_spec(4, width = "7em") %>%
  column_spec(5, width = "7em") %>%
  column_spec(6, width = "7em") %>%
  column_spec(7, width = "7em") %>%
  column_spec(8, width = "7em")

save_kable(table_latex_2, file = "tables/SI_Table_8.tex")

# Functions ##########################################
create_emm_plot <- function(emm_data, y_label, y_min = NULL, y_max = NULL, step_size = NULL, show_legend = FALSE, show_y_label = TRUE) {
  
  # Convert Treatment_Klima to factor with labels
  emm_data <- emm_data %>%
    mutate(Treatment_Klima = factor(Treatment_Klima, levels = c(0, 1), labels = c("Control", "Treatment")))
  
  
  # Create plot
  p <- ggplot(emm_data, aes(x = Treatment_Klima, y = emmean, color = Treatment_Klima)) +
    geom_point(size = 3) +
    geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8) +
    scale_color_manual(values = c("Control" = "#4059AD", "Treatment" = "#CCA43B")) +
    labs(
      y = if (show_y_label) y_label else NULL
    ) +
    theme_minimal(base_size = 13) +
    theme(
      legend.position = if (show_legend) "right" else "none",
      plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
      axis.title.x = element_blank(),
      axis.title.y = if (show_y_label) element_text() else element_blank(),
      legend.text = element_text(color = "black"),
      axis.text = element_text(size = 12, color = "black"),
      axis.title = element_text(size = 12),
      panel.background = element_rect(fill = "white", color = NA),
      plot.background = element_rect(fill = "white", color = NA)
    )
  
  # Custom y-limits
  if (!is.null(y_min) && !is.null(y_max)) {
    p <- p +
      coord_cartesian(ylim = c(y_min, y_max)) +
      scale_y_continuous(breaks = seq(y_min, y_max, by = step_size))
  }
  
  return(p)
}

significance_stars <- function(p) {
  if (p < 0.001) {
    return("***")
  } else if (p < 0.01) {
    return("**")
  } else if (p < 0.05) {
    return("*")
  } else {
    return("")  # Not significant
  }
}
# Themes ##########################################

theme_env <- theme_light() +
  theme(legend.position = "right",
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text = element_text(size = 12, color = "black"),
        axis.title = element_text(size = 12),
        strip.text = element_text(size = 12),
        legend.title = element_text(size = 12),
        legend.text = element_text(size = 12),
        axis.text.x = element_text(angle = 0, hjust = 0.5),
        axis.title.x = element_text(margin = margin(t=6)))


labs_legend <- c("Control", "Treatment")  # Updated legend labels
vals_legend <- c("#4059AD", "#CCA43B")  # Keeping the same colors

x_lab_env <- "Importance of Climate and Environmental Protection"
legend_title <- "Group"
x_ticks <- seq(1, 10, 1)



# Regressions ########################################
model_control <- function(dependent_var, main_independent_var, control_variables, data) {
  lm_robust(as.formula(paste(dependent_var, "~", main_independent_var, "+", paste(control_variables, collapse = " + "))), data = data)
}


controls_3 <- c("environment", "left_right", "political_party_f")

stars<-c("." = 0.1,"*" =0.05,"**" =0.01, "***" =0.001)

## Balance Checks ################################################
### Wave 1 ######################################################
#gender_binary
bc_gender_binary <- lm_robust(Treatment_Klima ~ gender_binary, data = data_vote)
summary(bc_gender_binary)
# not significant

#age
bc_age <- lm_robust(age ~ Treatment_Klima, data = data_vote)
summary(bc_age)
# not significant

#Environment
bc_environment <- lm_robust(environment ~ Treatment_Klima, data = data)
summary(bc_environment)
#significant

# urban_rural_true
bc_urban_rural <- lm_robust(urban_rural_true ~ Treatment_Klima, data = data_vote)
summary(bc_urban_rural)
# not significant

#left_right
bc_left_right <- lm_robust(left_right ~ Treatment_Klima, data = data_vote)
summary(bc_left_right)
# Significant!!

#political_party_f
bc_political_party <- lm_robust(Treatment_Klima ~ political_party_f, data = data_vote)
summary(bc_political_party)
# not significant

#education_f
bc_education <- lm_robust(Treatment_Klima ~ education_f, data = data_vote)
summary(bc_education)
# not significant

# Q84 Trust in Judges
bc_Q84 <- lm_robust(Q84 ~ Treatment_Klima, data = data_vote)
summary(bc_Q84)
# not significant

# Q81 Knowledge of Court
bc_Q81 <- lm_robust(Q81 ~ Treatment_Klima, data = data_vote)
summary(bc_Q81)
# not significant

# Q226 Opinion on Court
bc_Q226 <- lm_robust(Q226 ~ Treatment_Klima, data = data_vote)
summary(bc_Q226)
# not significant

# Q83
bc_Q83 <- lm_robust(Q83 ~ Treatment_Klima, data = data_vote)
summary(bc_Q83)
# not significant

#### Export as Table #########################################
models_balance1 <- list(
  bc_age,
  bc_environment,
  bc_urban_rural,
  bc_left_right,
  bc_Q84,
  bc_Q81,
  bc_Q226,
  bc_Q83
)
texreg::htmlreg(lapply(models_balance1, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("Age", "Environment", "Urban_Rural", "left_right", "Trust Judges", "Knowledge Court", "Opinion Court", "Knowledge Case"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment"),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/Balance_Checks_1.html",
               use.packages = F,
               caption = "Balance Checks Wave 1"
)


### Wave 2 #####################################################
#gender_binary
bc_gender_binary <- lm_robust(Treatment_Klima ~ gender_binary, data = data_vote_fin2)
summary(bc_gender_binary)
# not significant

#age
bc_age <- lm_robust(age ~ Treatment_Klima, data = data_vote_fin2)
summary(bc_age)
# not significant

#environment
bc_environment <- lm_robust(environment ~ Treatment_Klima, data = data_vote_fin2)
summary(bc_environment)
#significant

# urban_rural_true
bc_urban_rural <- lm_robust(urban_rural_true ~ Treatment_Klima, data = data_vote_fin2)
summary(bc_urban_rural)
# not significant

#left_right
bc_left_right <- lm_robust(left_right ~ Treatment_Klima, data = data_vote_fin2)
summary(bc_left_right)
# Not significant!

#political_party_f
bc_political_party <- lm_robust(Treatment_Klima ~ political_party_f, data = data_vote_fin2)
summary(bc_political_party)
# Not significant

#education_f
bc_education <- lm_robust(Treatment_Klima ~ education_f, data = data_vote_fin2)
summary(bc_education)
# not significant

# Q84
bc_Q84 <- lm_robust(Q84 ~ Treatment_Klima, data = data_vote_fin2)
summary(bc_Q84)
# not significant

# Q81
bc_Q81 <- lm_robust(Q81 ~ Treatment_Klima, data = data_vote_fin2)
summary(bc_Q81)
# not significant

# Q226
bc_Q226 <- lm_robust(Q226 ~ Treatment_Klima, data = data_vote_fin2)
summary(bc_Q226)
# not significant

# Q93
bc_Q83 <- lm_robust(Q83 ~ Treatment_Klima, data = data_vote_fin2)
summary(bc_Q83)
# not significant

#### Export as Table #########################################
models_balance2 <- list(
  bc_age,
  bc_environment,
  bc_urban_rural,
  bc_left_right,
  bc_Q84,
  bc_Q81,
  bc_Q226,
  bc_Q83
)
texreg::htmlreg(lapply(models_balance2, function(model) texreg::extract(model, include.ci = FALSE)),
                digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
                custom.model.names = c("Age", "Environment", "Urban_Rural", "left_right", "Trust Judges", "Knowledge Court", "Opinion Court", "Knowledge Case"),
                fontsize = "small", longtable = T, no.margin = T,
                custom.coef.names = c("Intercept",
                                      "Treatment"),
                custom.gof.names =c(NA, NA, "Observations", NA),
                reorder.gof =c(3, 1, 2, 4),
                file = "tables/Balance_Checks_2.html",
                use.packages = F,
                caption = "Balance Checks Wave 2"
)


## No Controls, No Interactions ###################################
dep_98 <- lm_robust(Q98 ~ Treatment_Klima, data = data_vote)
dep_227 <- lm_robust(Q227 ~ Treatment_Klima, data = data_vote)
dep_217 <- lm_robust(Q217 ~ Treatment_Klima, data = data_vote)
dep_223 <- lm_robust(Q223 ~ Treatment_Klima, data = data_vote)
dep_100 <- lm_robust(Q100 ~ Treatment_Klima, data = data_vote)

dep_102_1 <- lm_robust(Q102_1 ~ Treatment_Klima, data = data_vote)
dep_102_2 <- lm_robust(Q102_2 ~ Treatment_Klima, data = data_vote)
dep_102_3 <- lm_robust(Q102_3 ~ Treatment_Klima, data = data_vote)
dep_102_4 <- lm_robust(Q102_4 ~ Treatment_Klima, data = data_vote)

dep_intended_vote_no <- lm_robust(merged_intended_vote_no ~ Treatment_Klima, data = data_vote)
dep_intended_vote_undecided <- lm_robust(merged_intended_vote_undecided ~ Treatment_Klima, data = data_vote)
dep_intended_vote_yes <- lm_robust(merged_intended_vote_yes ~ Treatment_Klima, data = data_vote)
dep_intended_vote_2 <- lm_robust(merged_intended_vote_binary ~ Treatment_Klima, data = data_vote_fin2)
dep_revealed_vote <- lm_robust(vote_binary_pv ~ Treatment_Klima, data = data_vote_fin2)

### Export as Table #############################################
models_comp <- list(
  dep_98,
  dep_227,
  dep_217,
  dep_223,
  dep_100,
  dep_102_1,
  dep_102_2,
  dep_102_3,
  dep_102_4,
  dep_intended_vote_no,
  dep_intended_vote_undecided,
  dep_intended_vote_yes,
  dep_intended_vote_2,
  dep_revealed_vote
)

texreg::htmlreg(
  lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
  digits = 3,
  stars = c(0.001, 0.01, 0.05, 0.1),
  custom.model.names = c(
    "DV1", "DV2", "DV3", "DV4", "DV5",
    "DV6", "DV7", "DV8", "DV9", "DV10",
    "DV11", "DV12", "DV13", "DV14"
  ),
  fontsize = "small",
  longtable = TRUE,
  no.margin = TRUE,
  custom.coef.names = c("Intercept", "Treatment"),
  custom.gof.names = c(NA, NA, "Observations", NA),
  reorder.gof = c(3, 1, 2, 4),
  file = "tables/No_Controls.html",
  use.packages = FALSE,
  caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy,
  DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces,
  DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)


## No Controls, Interactions ###################################
### Political Orientation #####################################
dep_98_int_lr <- lm_robust(Q98 ~ Treatment_Klima * left_right, data = data_vote)
dep_227_int_lr <- lm_robust(Q227 ~ Treatment_Klima * left_right, data = data_vote)
dep_217_int_lr <- lm_robust(Q217 ~ Treatment_Klima * left_right, data = data_vote)
dep_223_int_lr <- lm_robust(Q223 ~ Treatment_Klima * left_right, data = data_vote)
dep_100_int_lr <- lm_robust(Q100 ~ Treatment_Klima * left_right, data = data_vote)

dep_102_1_int_lr <- lm_robust(Q102_1 ~ Treatment_Klima * left_right, data = data_vote)
dep_102_2_int_lr <- lm_robust(Q102_2 ~ Treatment_Klima * left_right, data = data_vote)
dep_102_3_int_lr <- lm_robust(Q102_3 ~ Treatment_Klima * left_right, data = data_vote)
dep_102_4_int_lr <- lm_robust(Q102_4 ~ Treatment_Klima * left_right, data = data_vote)

dep_intended_vote_no_int_lr <- lm_robust(merged_intended_vote_no ~ Treatment_Klima * left_right, data = data_vote)
dep_intended_vote_undecided_int_lr <- lm_robust(merged_intended_vote_undecided ~ Treatment_Klima * left_right, data = data_vote)
dep_intended_vote_yes_int_lr <- lm_robust(merged_intended_vote_yes ~ Treatment_Klima * left_right, data = data_vote)

dep_intended_vote_2_int_lr <- lm_robust(merged_intended_vote_binary ~ Treatment_Klima * left_right, data = data_vote_fin2)
dep_revealed_vote_int_lr <- lm_robust(vote_binary_pv ~ Treatment_Klima * left_right, data = data_vote_fin2)

#### Export as Table ###########################################
models_comp_int_lr <- list(
  dep_98_int_lr,
  dep_227_int_lr,
  dep_217_int_lr,
  dep_223_int_lr,
  dep_100_int_lr,
  dep_102_1_int_lr,
  dep_102_2_int_lr,
  dep_102_3_int_lr,
  dep_102_4_int_lr,
  dep_intended_vote_no_int_lr,
  dep_intended_vote_undecided_int_lr,
  dep_intended_vote_yes_int_lr,
  dep_intended_vote_2_int_lr,
  dep_revealed_vote_int_lr
)

texreg::htmlreg(
      lapply(models_comp_int_lr, function(model) texreg::extract(model, include.ci = FALSE)),
      digits = 3,
      stars = c(0.001, 0.01, 0.05, 0.1),
      custom.model.names = c(
        "DV1", "DV2", "DV3", "DV4", "DV5",
        "DV6", "DV7", "DV8", "DV9", "DV10",
        "DV11", "DV12", "DV13", "DV14"
      ),
      fontsize = "small",
      longtable = TRUE,
      no.margin = TRUE,
      custom.coef.names = c("Intercept",
                            "Treatment",
                            #Control
                            "Political Orientation",
                            #Interaction
                            "Treatment × Political Orientation"),
      groups = list( "Controls" = 3:3, "Interaction" = 4:4),
      custom.gof.names = c(NA, NA, "Observations", NA),
      reorder.gof = c(3, 1, 2, 4),
      file = "tables/No_controls_left_right.html",
      use.packages = FALSE,
      caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy,
      DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces,
      DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
 )
### Environment ################################################
dep_98_int_env <- lm_robust(Q98 ~ Treatment_Klima * environment, data = data_vote)
dep_227_int_env <- lm_robust(Q227 ~ Treatment_Klima * environment, data = data_vote)
dep_217_int_env <- lm_robust(Q217 ~ Treatment_Klima * environment, data = data_vote)
dep_223_int_env <- lm_robust(Q223 ~ Treatment_Klima * environment, data = data_vote)
dep_100_int_env <- lm_robust(Q100 ~ Treatment_Klima * environment, data = data_vote)

dep_102_1_int_env <- lm_robust(Q102_1 ~ Treatment_Klima * environment, data = data_vote)
dep_102_2_int_env <- lm_robust(Q102_2 ~ Treatment_Klima * environment, data = data_vote)
dep_102_3_int_env <- lm_robust(Q102_3 ~ Treatment_Klima * environment, data = data_vote)
dep_102_4_int_env <- lm_robust(Q102_4 ~ Treatment_Klima * environment, data = data_vote)

dep_intended_vote_no_int_env <- lm_robust(merged_intended_vote_no ~ Treatment_Klima * environment, data = data_vote)
dep_intended_vote_undecided_int_env <- lm_robust(merged_intended_vote_undecided ~ Treatment_Klima * environment, data = data_vote)
dep_intended_vote_yes_int_env <- lm_robust(merged_intended_vote_yes ~ Treatment_Klima * environment, data = data_vote)

dep_intended_vote_2_int_env <- lm_robust(merged_intended_vote_binary ~ Treatment_Klima * environment, data = data_vote_fin2)
dep_revealed_vote_int_env <- lm_robust(vote_binary_pv ~ Treatment_Klima * environment, data = data_vote_fin2)

#### Export as Table ###########################################
models_comp_int_env <- list(
  dep_98_int_env,
  dep_227_int_env,
  dep_217_int_env,
  dep_223_int_env,
  dep_100_int_env,
  dep_102_1_int_env,
  dep_102_2_int_env,
  dep_102_3_int_env,
  dep_102_4_int_env,
  dep_intended_vote_no_int_env,
  dep_intended_vote_undecided_int_env,
  dep_intended_vote_yes_int_env,
  dep_intended_vote_2_int_env,
  dep_revealed_vote_int_env
)

texreg::htmlreg(
  lapply(models_comp_int_env, function(model) texreg::extract(model, include.ci = FALSE)),
  digits = 3,
  stars = c(0.001, 0.01, 0.05, 0.1),
  custom.model.names = c(
    "DV1", "DV2", "DV3", "DV4", "DV5",
    "DV6", "DV7", "DV8", "DV9", "DV10",
    "DV11", "DV12", "DV13", "DV14"
  ),
  fontsize = "small",
  longtable = TRUE,
  no.margin = TRUE,
  custom.coef.names = c("Intercept",
                        "Treatment",
                        #Control
                        "Environment",
                        #Interaction
                        "Treatment × Environment"),
  groups = list( "Controls" = 3:3, "Interaction" = 4:4),
  custom.gof.names = c(NA, NA, "Observations", NA),
  reorder.gof = c(3, 1, 2, 4),
  file = "tables/No_controls_environment.html",
  use.packages = FALSE,
  caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy,
      DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces,
      DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)

### Knowledge of Case ##############################################
dep_98_int_Q83 <- lm_robust(Q98 ~ Treatment_Klima * Q83, data = data_vote)
dep_227_int_Q83 <- lm_robust(Q227 ~ Treatment_Klima * Q83, data = data_vote)
dep_217_int_Q83 <- lm_robust(Q217 ~ Treatment_Klima * Q83, data = data_vote)
dep_223_int_Q83 <- lm_robust(Q223 ~ Treatment_Klima * Q83, data = data_vote)
dep_100_int_Q83 <- lm_robust(Q100 ~ Treatment_Klima * Q83, data = data_vote)

dep_102_1_int_Q83 <- lm_robust(Q102_1 ~ Treatment_Klima * Q83, data = data_vote)
dep_102_2_int_Q83 <- lm_robust(Q102_2 ~ Treatment_Klima * Q83, data = data_vote)
dep_102_3_int_Q83 <- lm_robust(Q102_3 ~ Treatment_Klima * Q83, data = data_vote)
dep_102_4_int_Q83 <- lm_robust(Q102_4 ~ Treatment_Klima * Q83, data = data_vote)

dep_intended_vote_no_int_Q83 <- lm_robust(merged_intended_vote_no ~ Treatment_Klima * Q83, data = data_vote)
dep_intended_vote_undecided_int_Q83 <- lm_robust(merged_intended_vote_undecided ~ Treatment_Klima * Q83, data = data_vote)
dep_intended_vote_yes_int_Q83 <- lm_robust(merged_intended_vote_yes ~ Treatment_Klima * Q83, data = data_vote)

dep_intended_vote_2_int_Q83 <- lm_robust(merged_intended_vote_binary ~ Treatment_Klima * Q83, data = data_vote_fin2)
dep_revealed_vote_int_Q83 <- lm_robust(vote_binary_pv ~ Treatment_Klima * Q83, data = data_vote_fin2)

#### Export as Table ###########################################
models_comp_int_Q83 <- list(
  dep_98_int_Q83,
  dep_227_int_Q83,
  dep_217_int_Q83,
  dep_223_int_Q83,
  dep_100_int_Q83,
  dep_102_1_int_Q83,
  dep_102_2_int_Q83,
  dep_102_3_int_Q83,
  dep_102_4_int_Q83,
  dep_intended_vote_no_int_Q83,
  dep_intended_vote_undecided_int_Q83,
  dep_intended_vote_yes_int_Q83,
  dep_intended_vote_2_int_Q83,
  dep_revealed_vote_int_Q83
)

texreg::htmlreg(
  lapply(models_comp_int_Q83, function(model) texreg::extract(model, include.ci = FALSE)),
  digits = 3,
  stars = c(0.001, 0.01, 0.05, 0.1),
  custom.model.names = c(
    "DV1", "DV2", "DV3", "DV4", "DV5",
    "DV6", "DV7", "DV8", "DV9", "DV10",
    "DV11", "DV12", "DV13", "DV14"
  ),
  fontsize = "small",
  longtable = TRUE,
  no.margin = TRUE,
  custom.coef.names = c("Intercept",
                        "Treatment",
                        #Control
                        "Knowledge of Case",
                        #Interaction
                        "Treatment × Knowledge of Case"),
  groups = list( "Controls" = 3:3, "Interaction" = 4:4),
  custom.gof.names = c(NA, NA, "Observations", NA),
  reorder.gof = c(3, 1, 2, 4),
  file = "tables/No_controls_knowledge.html",
  use.packages = FALSE,
  caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy,
      DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces,
      DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)


### Opinion on Court ##########################################
dep_98_int_Q226 <- lm_robust(Q98 ~ Treatment_Klima * Q226, data = data_vote)
dep_227_int_Q226 <- lm_robust(Q227 ~ Treatment_Klima * Q226, data = data_vote)
dep_217_int_Q226 <- lm_robust(Q217 ~ Treatment_Klima * Q226, data = data_vote)
dep_223_int_Q226 <- lm_robust(Q223 ~ Treatment_Klima * Q226, data = data_vote)
dep_100_int_Q226 <- lm_robust(Q100 ~ Treatment_Klima * Q226, data = data_vote)

dep_102_1_int_Q226 <- lm_robust(Q102_1 ~ Treatment_Klima * Q226, data = data_vote)
dep_102_2_int_Q226 <- lm_robust(Q102_2 ~ Treatment_Klima * Q226, data = data_vote)
dep_102_3_int_Q226 <- lm_robust(Q102_3 ~ Treatment_Klima * Q226, data = data_vote)
dep_102_4_int_Q226 <- lm_robust(Q102_4 ~ Treatment_Klima * Q226, data = data_vote)

dep_intended_vote_no_int_Q226 <- lm_robust(merged_intended_vote_no ~ Treatment_Klima * Q226, data = data_vote)
dep_intended_vote_undecided_int_Q226 <- lm_robust(merged_intended_vote_undecided ~ Treatment_Klima * Q226, data = data_vote)
dep_intended_vote_yes_int_Q226 <- lm_robust(merged_intended_vote_yes ~ Treatment_Klima * Q226, data = data_vote)

dep_intended_vote_2_int_Q226 <- lm_robust(merged_intended_vote_binary ~ Treatment_Klima * Q226, data = data_vote_fin2)
dep_revealed_vote_int_Q226 <- lm_robust(vote_binary_pv ~ Treatment_Klima * Q226, data = data_vote_fin2)

#### Export as Table ###########################################
models_comp_int_Q226 <- list(
  dep_98_int_Q226,
  dep_227_int_Q226,
  dep_217_int_Q226,
  dep_223_int_Q226,
  dep_100_int_Q226,
  dep_102_1_int_Q226,
  dep_102_2_int_Q226,
  dep_102_3_int_Q226,
  dep_102_4_int_Q226,
  dep_intended_vote_no_int_Q226,
  dep_intended_vote_undecided_int_Q226,
  dep_intended_vote_yes_int_Q226,
  dep_intended_vote_2_int_Q226,
  dep_revealed_vote_int_Q226
)

texreg::htmlreg(
  lapply(models_comp_int_Q226, function(model) texreg::extract(model, include.ci = FALSE)),
  digits = 3,
  stars = c(0.001, 0.01, 0.05, 0.1),
  custom.model.names = c(
    "DV1", "DV2", "DV3", "DV4", "DV5",
    "DV6", "DV7", "DV8", "DV9", "DV10",
    "DV11", "DV12", "DV13", "DV14"
  ),
  fontsize = "small",
  longtable = TRUE,
  no.margin = TRUE,
  custom.coef.names = c("Intercept",
                        "Treatment",
                        #Control
                        "Opinion on Court",
                        #Interaction
                        "Treatment × Opinion on Court"),
  groups = list( "Controls" = 3:3, "Interaction" = 4:4),
  custom.gof.names = c(NA, NA, "Observations", NA),
  reorder.gof = c(3, 1, 2, 4),
  file = "tables/No_controls_opinion.html",
  use.packages = FALSE,
  caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy,
      DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces,
      DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)

### Trust in Judges ###########################################
dep_98_int_Q84 <- lm_robust(Q98 ~ Treatment_Klima * Q84, data = data_vote)
dep_227_int_Q84 <- lm_robust(Q227 ~ Treatment_Klima * Q84, data = data_vote)
dep_217_int_Q84 <- lm_robust(Q217 ~ Treatment_Klima * Q84, data = data_vote)
dep_223_int_Q84 <- lm_robust(Q223 ~ Treatment_Klima * Q84, data = data_vote)
dep_100_int_Q84 <- lm_robust(Q100 ~ Treatment_Klima * Q84, data = data_vote)

dep_102_1_int_Q84 <- lm_robust(Q102_1 ~ Treatment_Klima * Q84, data = data_vote)
dep_102_2_int_Q84 <- lm_robust(Q102_2 ~ Treatment_Klima * Q84, data = data_vote)
dep_102_3_int_Q84 <- lm_robust(Q102_3 ~ Treatment_Klima * Q84, data = data_vote)
dep_102_4_int_Q84 <- lm_robust(Q102_4 ~ Treatment_Klima * Q84, data = data_vote)

dep_intended_vote_no_int_Q84 <- lm_robust(merged_intended_vote_no ~ Treatment_Klima * Q84, data = data_vote)
dep_intended_vote_undecided_int_Q84 <- lm_robust(merged_intended_vote_undecided ~ Treatment_Klima * Q84, data = data_vote)
dep_intended_vote_yes_int_Q84 <- lm_robust(merged_intended_vote_yes ~ Treatment_Klima * Q84, data = data_vote)

dep_intended_vote_2_int_Q84 <- lm_robust(merged_intended_vote_binary ~ Treatment_Klima * Q84, data = data_vote_fin2)
dep_revealed_vote_int_Q84 <- lm_robust(vote_binary_pv ~ Treatment_Klima * Q84, data = data_vote_fin2)

#### Export as Table ###########################################
models_comp_int_Q84 <- list(
  dep_98_int_Q84,
  dep_227_int_Q84,
  dep_217_int_Q84,
  dep_223_int_Q84,
  dep_100_int_Q84,
  dep_102_1_int_Q84,
  dep_102_2_int_Q84,
  dep_102_3_int_Q84,
  dep_102_4_int_Q84,
  dep_intended_vote_no_int_Q84,
  dep_intended_vote_undecided_int_Q84,
  dep_intended_vote_yes_int_Q84,
  dep_intended_vote_2_int_Q84,
  dep_revealed_vote_int_Q84
)

texreg::htmlreg(
  lapply(models_comp_int_Q84, function(model) texreg::extract(model, include.ci = FALSE)),
  digits = 3,
  stars = c(0.001, 0.01, 0.05, 0.1),
  custom.model.names = c(
    "DV1", "DV2", "DV3", "DV4", "DV5",
    "DV6", "DV7", "DV8", "DV9", "DV10",
    "DV11", "DV12", "DV13", "DV14"
  ),
  fontsize = "small",
  longtable = TRUE,
  no.margin = TRUE,
  custom.coef.names = c("Intercept",
                        "Treatment",
                        #Control
                        "Trust in Judges",
                        #Interaction
                        "Treatment × Trust in Judges"),
  groups = list( "Controls" = 3:3, "Interaction" = 4:4),
  custom.gof.names = c(NA, NA, "Observations", NA),
  reorder.gof = c(3, 1, 2, 4),
  file = "tables/No_controls_trust.html",
  use.packages = FALSE,
  caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy,
      DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces,
      DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)


## Controls, No Interactions ######################################
dep_98_control_3 <- model_control("Q98", "Treatment_Klima", controls_3, data_vote)
summary(dep_98_control_3)

# Q227
dep_227_control_3 <- model_control("Q227", "Treatment_Klima", controls_3, data_vote)
summary(dep_227_control_3)

# Q217
dep_217_control_3 <- model_control("Q217", "Treatment_Klima", controls_3, data_vote)
summary(dep_217_control_3)

# Q223
dep_223_control_3 <- model_control("Q223", "Treatment_Klima", controls_3, data_vote)
summary(dep_223_control_3)

# Q100
dep_100_control_3 <- model_control("Q100", "Treatment_Klima", controls_3, data_vote)
summary(dep_100_control_3)

# Q102
dep_102_1_control_3 <- model_control("Q102_1", "Treatment_Klima", controls_3, data_vote)
summary(dep_102_1_control_3)

dep_102_2_control_3 <- model_control("Q102_2", "Treatment_Klima", controls_3, data_vote)
summary(dep_102_2_control_3)

dep_102_3_control_3 <- model_control("Q102_3", "Treatment_Klima", controls_3, data_vote)
summary(dep_102_3_control_3)

dep_102_4_control_3 <- model_control("Q102_4", "Treatment_Klima", controls_3, data_vote)
summary(dep_102_4_control_3)


#Voting
dep_intended_vote_no_control_3 <- model_control("merged_intended_vote_no", "Treatment_Klima", controls_3, data_vote)
dep_intended_vote_undecided_control_3 <- model_control("merged_intended_vote_undecided", "Treatment_Klima", controls_3, data_vote)
dep_intended_vote_yes_control_3 <- model_control("merged_intended_vote_yes", "Treatment_Klima", controls_3, data_vote)
dep_intended_vote_2_control_3 <- model_control("merged_intended_vote_binary", "Treatment_Klima", controls_3, data_vote_fin2)
dep_revealed_vote_control_3 <- model_control("vote_binary_pv", "Treatment_Klima", controls_3, data_vote_fin2)

### SI Table 9 #############################################
models_comp <- list(
  dep_98_control_3,
  dep_227_control_3,
  dep_217_control_3,
  dep_223_control_3,
  dep_100_control_3
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV1", "DV2", "DV3", "DV4", "DV5"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU"),
               groups = list("Controls" = 3:16),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_9.tex",
               use.packages = F,
               caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy "
)

###Plot Fig. 1 in Main Manuscript ######################################################
# Fig. 1 shows the information treatment effects on citizens’ perceived legitimacy of the ECtHR ruling
# in the case KlimaSeniorinnen and Others v. Switzerland

#Q98
means_dat <- emmeans(dep_98_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_1 <- create_emm_plot(means_dat, y_label = "Estimated Mean", y_min = 3.25, y_max = 4.5, step_size = 0.25, FALSE, TRUE) +
  ggtitle("Support\nfor court ruling") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_98_control_3$coefficients["Treatment_Klima"], significance_stars(dep_98_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0
  )

#Q227
means_dat <- emmeans(dep_227_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_2 <- create_emm_plot(means_dat, y_label = "Estimated Rating", y_min = 3.25, y_max = 4.5,step_size = 0.25, FALSE, FALSE) +
  ggtitle("Fairness\nof court ruling") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_227_control_3$coefficients["Treatment_Klima"], significance_stars(dep_227_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0 
  )

#Q217
means_dat <- emmeans(dep_217_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_3 <- create_emm_plot(means_dat, y_label = "Estimated Rating", y_min = 3.25, y_max = 4.5, step_size = 0.25, FALSE, FALSE) +
  ggtitle("Appropriateness\nof court ruling") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_217_control_3$coefficients["Treatment_Klima"], significance_stars(dep_217_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0 
  )

emm_plot_1 <- emm_plot_1 + labs(tag = "a)") + theme(plot.tag = element_text(face = "bold", size = 14))
emm_plot_2 <- emm_plot_2 + labs(tag = "b)") + theme(plot.tag = element_text(face = "bold", size = 14))
emm_plot_3 <- emm_plot_3 + labs(tag = "c)") + theme(plot.tag = element_text(face = "bold", size = 14))

final_plot <- (emm_plot_1 + emm_plot_2 + emm_plot_3) + 
  plot_annotation(
    title = "Attitudes towards court ruling", 
    tag_levels = "a", 
    tag_suffix = ")",
    theme = theme(
      plot.title = element_text(face = "bold", size = 16)
    )
  )
ggsave(filename = "plots/Figure_1.png", plot = final_plot, width = 8.5, height =3)

###Plot Fig. 2 in Main Manuscript ######################################################
# Fig. 2 shows the information treatment effects on citizens’ support for more ambitious climate policies in Switzerland
# to comply with the court ruling (Fig.2a) and citizens’ perceived sufficiency of Swiss climate policy ambition considering the court decision (Fig. 2b).

#Q223
means_dat <- emmeans(dep_223_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_1 <- create_emm_plot(means_dat, y_label = "Estimated Mean", y_min = 3.0, y_max = 4.9, step_size = 0.5, FALSE, TRUE) +
  ggtitle("More ambitious\nclimate policy for CH") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_223_control_3$coefficients["Treatment_Klima"], significance_stars(dep_223_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0 
  )

#Q100
means_dat <- emmeans(dep_100_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_2 <- create_emm_plot(means_dat, y_label = "Estimated Rating", y_min = 3.0, y_max = 4.9, step_size = 0.5, FALSE, FALSE) +
  ggtitle("Sufficiency of current\nCH climate policy") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_100_control_3$coefficients["Treatment_Klima"], significance_stars(dep_100_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0 
  )


emm_plot_1 <- emm_plot_1 + labs(tag = "a)") + theme(plot.tag = element_text(face = "bold", size = 14))
emm_plot_2 <- emm_plot_2 + labs(tag = "b)") + theme(plot.tag = element_text(face = "bold", size = 14))

final_plot <- (emm_plot_1 + emm_plot_2) + 
  plot_annotation(
    title = "Attitudes towards Climate Policy in Compliance\nwith Court Ruling", 
    tag_levels = "a", 
    tag_suffix = ")",
    theme = theme(
      plot.title = element_text(face = "bold", size = 16)
    )
  )
ggsave(filename = "plots/Figure_2.png", plot = final_plot, width = 6.5, height = 3.5)

### SI Table 10 ########################################
models_comp <- list(
  dep_102_1_control_3,
  dep_102_2_control_3,
  dep_102_3_control_3,
  dep_102_4_control_3
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV6", "DV7", "DV8", "DV9"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU"),
               groups = list("Controls" = 3:16),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_10.tex",
               use.packages = F,
               caption = "DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces "
)


### SI Table 11 ############################################
models_comp <- list(
  dep_intended_vote_no_control_3,
  dep_intended_vote_undecided_control_3,
  dep_intended_vote_yes_control_3,
  dep_intended_vote_2_control_3,
  dep_revealed_vote_control_3
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV10", "DV11", "DV12", "DV13", "DV14"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                      #Controls
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU"),
               groups = list("Controls" = 3:16),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_11.tex",
               use.packages = F,
               caption = "DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)

###Plot Fig. 3 in Main Manuscript ######################################################
# Fig. 3 shows the information treatment effects on citizens’ intended voting behavior before 
# (Wave 1 conducted between 5th of May until the 8th of June 2024) the public referendum on the Swiss Electricity law on the 9th of June 2024.

#Intended
means_dat <- emmeans(dep_intended_vote_no_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_1 <- create_emm_plot(means_dat, y_label = "Estimated share of votes", y_min = 0, y_max = 0.7, step_size = 0.2, FALSE, TRUE) +
  ggtitle("No") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_intended_vote_no_control_3$coefficients["Treatment_Klima"], significance_stars(dep_intended_vote_no_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0 
  )

#Undecided
means_dat <- emmeans(dep_intended_vote_undecided_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_2 <- create_emm_plot(means_dat, y_label = "Estimated Proportion", y_min = 0, y_max = 0.7,step_size = 0.2, FALSE, FALSE) +
  ggtitle("Undecided") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_intended_vote_undecided_control_3$coefficients["Treatment_Klima"], significance_stars(dep_intended_vote_undecided_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0 
  )

#Yes
means_dat <- emmeans(dep_intended_vote_yes_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_3 <- create_emm_plot(means_dat, y_label = "Estimated Proportion", y_min = 0, y_max = 0.7, step_size = 0.2, FALSE, FALSE) +
  ggtitle("Yes") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_intended_vote_yes_control_3$coefficients["Treatment_Klima"], significance_stars(dep_intended_vote_yes_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0 
  )

emm_plot_1 <- emm_plot_1 + labs(tag = "a)") + theme(plot.tag = element_text(face = "bold", size = 14))
emm_plot_2 <- emm_plot_2 + labs(tag = "b)") + theme(plot.tag = element_text(face = "bold", size = 14))
emm_plot_3 <- emm_plot_3 + labs(tag = "c)") + theme(plot.tag = element_text(face = "bold", size = 14))

final_plot <- (emm_plot_1 + emm_plot_2 + emm_plot_3) + 
  plot_annotation(
    title = "Treatment Effect on Intended Voting Behaviour", 
    tag_levels = "a", 
    tag_suffix = ")",
    theme = theme(
      plot.title = element_text(face = "bold", size = 16)
    )
  )
ggsave(filename = "plots/Figure_3.png", plot = final_plot, width = 8.8, height =3.2)

###Plot Fig. 4 in Main Manuscript ######################################################
#Fig. 4a shows the information treatment effects on the intended share of yes votes 
# (as proportion of the sum of yes and no votes) before the public referendum on the Swiss Electricity law on the 9th of June 2024
# Fig. 4b shows the revealed share of yes votes in the referendum among actual voters, as indicated by citizens in Wave 2 after the referendum

#Intended Vote (actual voters)
means_dat <- emmeans(dep_intended_vote_2_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_1 <- create_emm_plot(means_dat, y_label = "Estimated share of yes-Votes", y_min = 0.7, y_max = 1,step_size = 0.1, FALSE, TRUE) +
  ggtitle("Intended share of yes-Votes\namong actual voters") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_intended_vote_2_control_3$coefficients["Treatment_Klima"], significance_stars(dep_intended_vote_2_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0 
  )

#Revealed Vote
means_dat <- emmeans(dep_revealed_vote_control_3, "Treatment_Klima", cov.keep = "Treatment_Klima") %>% as.data.frame()

emm_plot_2 <- create_emm_plot(means_dat, y_label = "Estimated share of yes-Votes", y_min = 0.7, y_max = 1, step_size = 0.1, FALSE, FALSE) +
  ggtitle("Reported share of yes-Votes\namong actual voters") +
  theme(
    plot.title = element_text(hjust = 0.5)
  )+ 
  geom_signif(
    comparisons = list(c("Control", "Treatment")),  
    annotations = sprintf("%.4f %s", dep_revealed_vote_control_3$coefficients["Treatment_Klima"], significance_stars(dep_revealed_vote_control_3$p.value["Treatment_Klima"])),           
    color = "black",
    size = 0
  )

emm_plot_2 <- emm_plot_2 + labs(tag = "b)") + theme(plot.tag = element_text(face = "bold", size = 14))
emm_plot_3 <- emm_plot_3 + labs(tag = "c)") + theme(plot.tag = element_text(face = "bold", size = 14))

final_plot <- (emm_plot_1 + emm_plot_2) + 
  plot_annotation(
    title = "Treatment Effect on Intended and Revealed Voting\nBehaviour", 
    tag_levels = "a", 
    tag_suffix = ")",
    theme = theme(
      plot.title = element_text(face = "bold", size = 16)
    )
  )
ggsave(filename = "plots/Figure_4.png", plot = final_plot, width = 6.5, height =3.5)

## Controls, Interaction ############################################

### Political Orientation ####################################################

dep_98_int_lr_c <- model_control("Q98", "Treatment_Klima * left_right", controls_3, data_vote)
dep_227_int_lr_c <- model_control("Q227", "Treatment_Klima * left_right", controls_3, data_vote)
dep_217_int_lr_c <- model_control("Q217", "Treatment_Klima * left_right", controls_3, data_vote)
dep_223_int_lr_c <- model_control("Q223", "Treatment_Klima * left_right", controls_3, data_vote)
dep_100_int_lr_c <- model_control("Q100", "Treatment_Klima * left_right", controls_3, data_vote)

dep_102_1_int_lr_c <- model_control("Q102_1", "Treatment_Klima * left_right", controls_3, data_vote)
dep_102_2_int_lr_c <- model_control("Q102_2", "Treatment_Klima * left_right", controls_3, data_vote)
dep_102_3_int_lr_c <- model_control("Q102_3", "Treatment_Klima * left_right", controls_3, data_vote)
dep_102_4_int_lr_c <- model_control("Q102_4", "Treatment_Klima * left_right", controls_3, data_vote)

dep_intended_vote_no_int_lr_c <- model_control("merged_intended_vote_no", "Treatment_Klima * left_right", controls_3, data_vote)
dep_intended_vote_undecided_int_lr_c <- model_control("merged_intended_vote_undecided", "Treatment_Klima * left_right", controls_3, data_vote)
dep_intended_vote_yes_int_lr_c <- model_control("merged_intended_vote_yes", "Treatment_Klima * left_right", controls_3, data_vote)
dep_intended_vote_2_int_lr_c <- model_control("merged_intended_vote_binary", "Treatment_Klima * left_right", controls_3, data_vote_fin2)
dep_revealed_vote_int_lr_c <- model_control("vote_binary_pv", "Treatment_Klima * left_right", controls_3, data_vote_fin2)

#### SI Table 12 ######################################################
models_comp <- list(
  dep_98_int_lr_c,
  dep_227_int_lr_c,
  dep_217_int_lr_c,
  dep_223_int_lr_c,
  dep_100_int_lr_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV1", "DV2", "DV3", "DV4", "DV5"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Political Orientation",
                                     "Environment",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Pol. Orientation"),
               groups = list("Controls" = 3:16, "Interaction" = 17:17),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_12.tex",
               use.packages = F,
               caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy "
)


#### SI Table 13 #####################################################
models_comp <- list(
  dep_102_1_int_lr_c,
  dep_102_2_int_lr_c,
  dep_102_3_int_lr_c,
  dep_102_4_int_lr_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV6", "DV7", "DV8", "DV9"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Political Orientation",
                                     "Environment",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Pol. Orientation"),
               groups = list("Controls" = 3:16, "Interaction" = 17:17),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_13.tex",
               use.packages = F,
               caption = "DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces "
)

#### SI Table 14 #########################################################

models_comp <- list(
  dep_intended_vote_no_int_lr_c,
  dep_intended_vote_undecided_int_lr_c,
  dep_intended_vote_yes_int_lr_c,
  dep_intended_vote_2_int_lr_c,
  dep_revealed_vote_int_lr_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV10", "DV11", "DV12", "DV13", "DV14"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Political Orientation",
                                     "Environment",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Pol. Orientation"),
               groups = list("Controls" = 3:16, "Interaction" = 17:17),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_14.tex",
               use.packages = F,
               caption = "DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)

### Environment ########################################################
dep_98_int_env_c <- model_control("Q98", "Treatment_Klima * environment", controls_3, data_vote)
dep_227_int_env_c <- model_control("Q227", "Treatment_Klima * environment", controls_3, data_vote)
dep_217_int_env_c <- model_control("Q217", "Treatment_Klima * environment", controls_3, data_vote)
dep_223_int_env_c <- model_control("Q223", "Treatment_Klima * environment", controls_3, data_vote)
dep_100_int_env_c <- model_control("Q100", "Treatment_Klima * environment", controls_3, data)
dep_102_1_int_env_c <- model_control("Q102_1", "Treatment_Klima * environment", controls_3, data_vote)
dep_102_2_int_env_c <- model_control("Q102_2", "Treatment_Klima * environment", controls_3, data_vote)
dep_102_3_int_env_c <- model_control("Q102_3", "Treatment_Klima * environment", controls_3, data_vote)
dep_102_4_int_env_c <- model_control("Q102_4", "Treatment_Klima * environment", controls_3, data_vote)

dep_intended_vote_no_int_env_c <- model_control("merged_intended_vote_no", "Treatment_Klima * environment", controls_3, data_vote)
dep_intended_vote_undecided_int_env_c <- model_control("merged_intended_vote_undecided", "Treatment_Klima * environment", controls_3, data_vote)
dep_intended_vote_yes_int_env_c <- model_control("merged_intended_vote_yes", "Treatment_Klima * environment", controls_3, data_vote)

dep_intended_vote_2_int_env_c <- model_control("merged_intended_vote_binary", "Treatment_Klima * environment", controls_3, data_vote_fin2)
dep_revealed_vote_int_env_c <- model_control("vote_binary_pv", "Treatment_Klima * environment", controls_3, data_vote_fin2)



#### SI Table 15 ##############################################
models_comp <- list(
  dep_98_int_env_c,
  dep_227_int_env_c,
  dep_217_int_env_c,
  dep_223_int_env_c,
  dep_100_int_env_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV1", "DV2", "DV3", "DV4", "DV5"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Environment"),
               groups = list("Controls" = 3:16, "Interaction" = 17:17),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_15.tex",
               use.packages = F,
               caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy "
)

#### Plot Fig. 5 in Main Manuscript ########################################################
# Fig. 5 shows the information treatment effects on citizens’ perceived legitimacy of the ECtHR ruling
# in the case “KlimaSeniorinnen and Others v. Switzerland” 
# moderated by citizens’ prior perceived importance of climate and environmental protection.

### Q98
means_dat <- emmeans(dep_98_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_98_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)


Q98_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  #geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL, col = Treatment_Klima), width = 0.1, position = position_dodge(0.1)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(2.5, 5), breaks = seq(2.5, 5, 0.5)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "Support for Court Ruling\n(1-7 Likert)",
    x = NULL,
    y = "Estimated Mean"
  ) + theme_env +
  theme(legend.position = "none") +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  ) +
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )

### Q227
means_dat <- emmeans(dep_227_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_227_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)

Q227_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  #geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL, col = Treatment_Klima), width = 0.1, position = position_dodge(0.1)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(2.5, 5), breaks = seq(2.5, 5, 0.5)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "Fairness of Court Ruling\n(1-7 Likert)",
    x = x_lab_env,
    y = NULL
  ) + theme_env +
  theme(legend.position = "none")  +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )


### Q217
means_dat <- emmeans(dep_217_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_217_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)

Q217_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(2.5, 5), breaks = seq(2.5, 5, 0.5)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "Appropriateness of Court Ruling\n(1-7 Likert)",
    x = NULL,
    y = NULL
  ) + theme_env  +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )

Q98_env <- Q98_env + labs(tag = "a)") + theme(plot.tag = element_text(face = "bold", size = 14))
Q227_env <- Q227_env + labs(tag = "b)") + theme(plot.tag = element_text(face = "bold", size = 14))
Q217_env <- Q217_env + labs(tag = "c)") + theme(plot.tag = element_text(face = "bold", size = 14))

final_plot <- (Q98_env + Q227_env + Q217_env) + 
  plot_annotation(
    title = "Treatment Effect on Attitudes towards Court Ruling by Environmental Awareness", 
    theme = theme(
      plot.title = element_text(face = "bold", size = 16)
    )
  )
ggsave(filename = "plots/Figure_5.png", plot = final_plot, width = 10.5, height =4)

#### Plot Fig. 6 in Main Manuscript ########################################################
# Fig. 6 shows the information treatment effects moderated by citizens’ prior perceived importance of 
# climate and environmental protection on citizens’ support for more ambitious climate policies in Switzerland
# to comply with the court ruling (Fig.6a) and citizens’ perceived sufficiency of Swiss climate policy ambition 
# considering the court decision (Fig. 6b).

#### Q223
means_dat <- emmeans(dep_223_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_223_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)


Q223_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(2.5, 5.5), breaks = seq(2.5, 5.5, 0.5)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "More Ambitious Climate Policy for CH\n(1-7 Likert)",
    x = NULL,
    y = "Estimated Mean"
  ) + theme_env +
  theme(legend.position = "none") +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )

#### Q100
means_dat <- emmeans(dep_100_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_100_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)

Q100_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(2.5, 5.5), breaks = seq(2.5, 5.5, 0.5)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "Sufficiency of Current CH Climate Policy\n(1-7 Likert)",
    x = NULL,
    y = NULL
  ) + theme_env +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.35,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )

Q223_env <- Q223_env + labs(tag = "a)") + theme(plot.tag = element_text(face = "bold", size = 14))
Q100_env <- Q100_env + labs(tag = "b)") + theme(plot.tag = element_text(face = "bold", size = 14))


final_plot <- (Q223_env + Q100_env) + 
  plot_annotation(
    title = "Treatment Effect on Attitudes towards Climate Policy in Compliance with\nCourt Ruling by Environmental Awareness", 
    caption = x_lab_env,
    theme = theme(
      plot.title = element_text(face = "bold", size = 16),
      plot.caption = element_text(face = "plain", size = 12, hjust = 0.42, margin = margin(t = 6))
    )
  )
ggsave(filename = "plots/Figure_6.png", plot = final_plot, width = 8.5, height =4)



#### SI Table 16 ##############################################
models_comp <- list(
  dep_102_1_int_env_c,
  dep_102_2_int_env_c,
  dep_102_3_int_env_c,
  dep_102_4_int_env_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV6", "DV7", "DV8", "DV9"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Environment"),
               groups = list("Controls" = 3:16, "Interaction" = 17:17),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_16.tex",
               use.packages = F,
               caption = "DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces "
)

#### SI Table 17 ##############################################

models_comp <- list(
  dep_intended_vote_no_int_env_c,
  dep_intended_vote_undecided_int_env_c,
  dep_intended_vote_yes_int_env_c,
  dep_intended_vote_2_int_env_c,
  dep_revealed_vote_int_env_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV10", "DV11", "DV12", "DV13", "DV14"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Environment"),
               groups = list("Controls" = 3:16, "Interaction" = 17:17),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_17.tex",
               use.packages = F,
               caption = "DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)

#### Plot Fig. 7 in Main Manuscript ########################################################
# Fig. 7 shows the information treatment effects moderated by citizens’ prior perceived importance
# of climate and environmental protection on citizens’ intended voting behavior before 
# (Wave 1 conducted between 5th of May until the 8th of June 2024)
# the public referendum on the Swiss Electricity law on the 9th of June 2024.


#No
means_dat <- emmeans(dep_intended_vote_no_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_intended_vote_no_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)

intended_vote_no_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  #geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL, col = Treatment_Klima), width = 0.1, position = position_dodge(0.1)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(0, 0.7), breaks = seq(0, 0.7, 0.2)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "No",
    x = NULL,
    y = "Estimated share of votes"
  ) + theme_env +
  theme(legend.position = "none") +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )

#Undecided
means_dat <- emmeans(dep_intended_vote_undecided_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_intended_vote_undecided_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)

intended_vote_undecided_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  #geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL, col = Treatment_Klima), width = 0.1, position = position_dodge(0.1)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(0, 0.7), breaks = seq(0, 0.7, 0.2)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "Undecided",
    x = x_lab_env,
    y = NULL
  ) + theme_env +
  theme(legend.position = "none") +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )

#Yes
means_dat <- emmeans(dep_intended_vote_yes_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_intended_vote_yes_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)

intended_vote_yes_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(0, 0.7), breaks = seq(0, 0.7, 0.2)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "Yes",
    x = NULL,
    y = NULL
  ) + theme_env +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )

intended_vote_no_env <- intended_vote_no_env + labs(tag = "a)") + theme(plot.tag = element_text(face = "bold", size = 14))
intended_vote_undecided_env <- intended_vote_undecided_env + labs(tag = "b)") + theme(plot.tag = element_text(face = "bold", size = 14))
intended_vote_yes_env <- intended_vote_yes_env + labs(tag = "c)") + theme(plot.tag = element_text(face = "bold", size = 14))

final_plot <- (intended_vote_no_env + intended_vote_undecided_env + intended_vote_yes_env) + 
  plot_annotation(
    title = "Treatment Effect on Intended Voting Behaviour by Environmental Awareness", 
    theme = theme(
      plot.title = element_text(face = "bold", size = 16)
    )
  )
ggsave(filename = "plots/Figure_7.png", plot = final_plot, width = 10.5, height =4)

#### Plot Fig. 8 in Main Manuscript ########################################################
# Fig. 8a shows the information treatment effects on the intended share of yes votes (as proportion of the sum of yes and no votes) 
# moderated by citizens’ prior perceived importance of climate and environmental protection on citizens’ intended voting behavior 
# before the public referendum on the Swiss Electricity law on the 9th of June 2024.
# Fig. 8b shows the moderated revealed share of yes votes in the referendum among actual voters, as indicated by citizens in Wave 2 after the referendum

#Intended Vote
means_dat <- emmeans(dep_intended_vote_2_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_intended_vote_2_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)

intended_vote_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(0.6, 1), breaks = seq(0.6, 1, 0.1)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "Intended share of yes-Votes\namong actual voters",
    x = NULL,
    y = "Estimated share of yes-Votes"
  ) + theme_env +
  theme(legend.position = "none")+
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )


#Revealed Vote
means_dat <- emmeans(dep_revealed_vote_int_env_c, "environment", by = "Treatment_Klima", cov.keep = c("Treatment_Klima", "environment"))
means_sig <- emmeans(dep_revealed_vote_int_env_c, ~ Treatment_Klima | environment)

contrast_results <- contrast(means_sig, method = "pairwise") %>%
  summary(infer = TRUE)

means_df <- as.data.frame(contrast_results)
means_dat_df <- as.data.frame(means_dat)

revealed_vote_env <- means_dat %>%
  as.data.frame() %>%
  mutate(Treatment_Klima = as.character(Treatment_Klima),
         environment = factor(environment, levels = c(0, 1), labels = c("Unimportant", "Important"))) %>%
  
  ggplot(aes(x = environment, y = emmean, group = Treatment_Klima, col = Treatment_Klima)) +
  geom_point(size = 3, position = position_dodge(0.3)) +
  geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.0, linewidth = 0.8, position = position_dodge(0.3)) +
  
  scale_x_discrete(labels = c("Unimportant", "Important")) +
  scale_y_continuous(limits = c(0.6, 1), breaks = seq(0.6, 1, 0.1)) +
  scale_color_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  scale_fill_manual(name = legend_title, values = vals_legend, labels = labs_legend) +
  
  guides(col = guide_legend(nrow = 2)) +
  
  labs(
    title = "Reported share of yes-Votes\namong actual voters",
    x = NULL,
    y = NULL
  ) + theme_env +
  
  annotate(
    "text",
    x = 1,
    y = max(c(means_dat_df[1, "emmean"], means_dat_df[3, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[1, "estimate"], significance_stars(means_df[1, "p.value"]))
  )+
  annotate(
    "text",
    x = 2,
    y = max(c(means_dat_df[2, "emmean"], means_dat_df[4, "emmean"])) + 0.1,
    label = sprintf("%.4f %s", -means_df[2, "estimate"], significance_stars(means_df[2, "p.value"]))
  )


intended_vote_env <- intended_vote_env + labs(tag = "a)") + theme(plot.tag = element_text(face = "bold", size = 14))
revealed_vote_env <- revealed_vote_env + labs(tag = "b)") + theme(plot.tag = element_text(face = "bold", size = 14))


final_plot <- (intended_vote_env + revealed_vote_env) + 
  plot_annotation(
    title = "Treatment Effect on Voting Behaviour by Environmental Awareness", 
    caption = x_lab_env,
    theme = theme(
      plot.title = element_text(face = "bold", size = 16),
      plot.caption = element_text(face = "plain", size = 12, hjust = 0.42, margin = margin(t = 6))
    )
  )
ggsave(filename = "plots/Figure_8.png", plot = final_plot, width = 8.5, height =3.5)


### Knowledge of Case ######################################
dep_98_int_Q83_c <- model_control("Q98", "Treatment_Klima * Q83", controls_3, data_vote)
dep_227_int_Q83_c <- model_control("Q227", "Treatment_Klima * Q83", controls_3, data_vote)
dep_217_int_Q83_c <- model_control("Q217", "Treatment_Klima * Q83", controls_3, data_vote)
dep_223_int_Q83_c <- model_control("Q223", "Treatment_Klima * Q83", controls_3, data_vote)
dep_100_int_Q83_c <- model_control("Q100", "Treatment_Klima * Q83", controls_3, data_vote)
dep_102_1_int_Q83_c <- model_control("Q102_1", "Treatment_Klima * Q83", controls_3, data_vote)
dep_102_2_int_Q83_c <- model_control("Q102_2", "Treatment_Klima * Q83", controls_3, data_vote)
dep_102_3_int_Q83_c <- model_control("Q102_3", "Treatment_Klima * Q83", controls_3, data_vote)
dep_102_4_int_Q83_c <- model_control("Q102_4", "Treatment_Klima * Q83", controls_3, data_vote)

dep_intended_vote_no_int_Q83_c <- model_control("merged_intended_vote_no", "Treatment_Klima * Q83", controls_3, data_vote)
dep_intended_vote_undecided_int_Q83_c <- model_control("merged_intended_vote_undecided", "Treatment_Klima * Q83", controls_3, data_vote)
dep_intended_vote_yes_int_Q83_c <- model_control("merged_intended_vote_yes", "Treatment_Klima * Q83", controls_3, data_vote)

dep_intended_vote_2_int_Q83_c <- model_control("merged_intended_vote_binary", "Treatment_Klima * Q83", controls_3, data_vote_fin2)
dep_revealed_vote_int_Q83_c <- model_control("vote_binary_pv", "Treatment_Klima * Q83", controls_3, data_vote_fin2)

#### SI Table 18 ##############################################
models_comp <- list(
  dep_98_int_Q83_c,
  dep_227_int_Q83_c,
  dep_217_int_Q83_c,
  dep_223_int_Q83_c,
  dep_100_int_Q83_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV1", "DV2", "DV3", "DV4", "DV5"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Knowledge of Case",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Knowledge of Case"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_18.tex",
               use.packages = F,
               caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy "
)


#### SI Table 19 ##############################################
models_comp <- list(
  dep_102_1_int_Q83_c,
  dep_102_2_int_Q83_c,
  dep_102_3_int_Q83_c,
  dep_102_4_int_Q83_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV6", "DV7", "DV8", "DV9"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Knowledge of Case",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Knowledge of Case"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_19.tex",
               use.packages = F,
               caption = "DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces "
)

#### SI Table 20 ##############################################

models_comp <- list(
  dep_intended_vote_no_int_Q83_c,
  dep_intended_vote_undecided_int_Q83_c,
  dep_intended_vote_yes_int_Q83_c,
  dep_intended_vote_2_int_Q83_c,
  dep_revealed_vote_int_Q83_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV10", "DV11", "DV12", "DV13", "DV14"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Knowledge of Case",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Knowledge of Case"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_20.tex",
               use.packages = F,
               caption = "DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)

### Opinion on Court ##########################################
dep_98_int_Q226_c <- model_control("Q98", "Treatment_Klima * Q226", controls_3, data_vote)
dep_227_int_Q226_c <- model_control("Q227", "Treatment_Klima * Q226", controls_3, data_vote)
dep_217_int_Q226_c <- model_control("Q217", "Treatment_Klima * Q226", controls_3, data_vote)
dep_223_int_Q226_c <- model_control("Q223", "Treatment_Klima * Q226", controls_3, data_vote)
dep_100_int_Q226_c <- model_control("Q100", "Treatment_Klima * Q226", controls_3, data_vote)
dep_102_1_int_Q226_c <- model_control("Q102_1", "Treatment_Klima * Q226", controls_3, data_vote)
dep_102_2_int_Q226_c <- model_control("Q102_2", "Treatment_Klima * Q226", controls_3, data_vote)
dep_102_3_int_Q226_c <- model_control("Q102_3", "Treatment_Klima * Q226", controls_3, data_vote)
dep_102_4_int_Q226_c <- model_control("Q102_4", "Treatment_Klima * Q226", controls_3, data_vote)

dep_intended_vote_no_int_Q226_c <- model_control("merged_intended_vote_no", "Treatment_Klima * Q226", controls_3, data_vote)
dep_intended_vote_undecided_int_Q226_c <- model_control("merged_intended_vote_undecided", "Treatment_Klima * Q226", controls_3, data_vote)
dep_intended_vote_yes_int_Q226_c <- model_control("merged_intended_vote_yes", "Treatment_Klima * Q226", controls_3, data_vote)

dep_intended_vote_2_int_Q226_c <- model_control("merged_intended_vote_binary", "Treatment_Klima * Q226", controls_3, data_vote_fin2)
dep_revealed_vote_int_Q226_c <- model_control("vote_binary_pv", "Treatment_Klima * Q226", controls_3, data_vote_fin2)

#### SI Table 21 ##########################################
models_comp <- list(
  dep_98_int_Q226_c,
  dep_227_int_Q226_c,
  dep_217_int_Q226_c,
  dep_223_int_Q226_c,
  dep_100_int_Q226_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV1", "DV2", "DV3", "DV4", "DV5"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Opinion on Court",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Opinion on Court"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_21.tex",
               use.packages = F,
               caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy "
)


#### SI Table 22 ##########################################
models_comp <- list(
  dep_102_1_int_Q226_c,
  dep_102_2_int_Q226_c,
  dep_102_3_int_Q226_c,
  dep_102_4_int_Q226_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV6", "DV7", "DV8", "DV9"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Opinion on Court",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Opinion on Court"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_22.tex",
               use.packages = F,
               caption = "DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces "
)

#### SI Table 23 ##########################################

models_comp <- list(
  dep_intended_vote_no_int_Q226_c,
  dep_intended_vote_undecided_int_Q226_c,
  dep_intended_vote_yes_int_Q226_c,
  dep_intended_vote_2_int_Q226_c,
  dep_revealed_vote_int_Q226_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV10", "DV11", "DV12", "DV13", "DV14"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Opinion on Court",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Opinion on Court"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_23.tex",
               use.packages = F,
               caption = "DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)

### Trust in Judges #################################################
dep_98_int_Q84_c <- model_control("Q98", "Treatment_Klima * Q84", controls_3, data_vote)
dep_227_int_Q84_c <- model_control("Q227", "Treatment_Klima * Q84", controls_3, data_vote)
dep_217_int_Q84_c <- model_control("Q217", "Treatment_Klima * Q84", controls_3, data_vote)
dep_223_int_Q84_c <- model_control("Q223", "Treatment_Klima * Q84", controls_3, data_vote)
dep_100_int_Q84_c <- model_control("Q100", "Treatment_Klima * Q84", controls_3, data_vote)
dep_102_1_int_Q84_c <- model_control("Q102_1", "Treatment_Klima * Q84", controls_3, data_vote)
dep_102_2_int_Q84_c <- model_control("Q102_2", "Treatment_Klima * Q84", controls_3, data_vote)
dep_102_3_int_Q84_c <- model_control("Q102_3", "Treatment_Klima * Q84", controls_3, data_vote)
dep_102_4_int_Q84_c <- model_control("Q102_4", "Treatment_Klima * Q84", controls_3, data_vote)

dep_intended_vote_no_int_Q84_c <- model_control("merged_intended_vote_no", "Treatment_Klima * Q84", controls_3, data_vote)
dep_intended_vote_undecided_int_Q84_c <- model_control("merged_intended_vote_undecided", "Treatment_Klima * Q84", controls_3, data_vote)
dep_intended_vote_yes_int_Q84_c <- model_control("merged_intended_vote_yes", "Treatment_Klima * Q84", controls_3, data_vote)

dep_intended_vote_2_int_Q84_c <- model_control("merged_intended_vote_binary", "Treatment_Klima * Q84", controls_3, data_vote_fin2)
dep_revealed_vote_int_Q84_c <- model_control("vote_binary_pv", "Treatment_Klima * Q84", controls_3, data_vote_fin2)

#### SI Table 24 ##########################################
models_comp <- list(
  dep_98_int_Q84_c,
  dep_227_int_Q84_c,
  dep_217_int_Q84_c,
  dep_223_int_Q84_c,
  dep_100_int_Q84_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV1", "DV2", "DV3", "DV4", "DV5"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Trust in Judges",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Trust in Judges"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_24.tex",
               use.packages = F,
               caption = "DV1: Support for court ruling, DV2: Fairness of court ruling, DV3: Appropriateness of court ruling, DV4: More ambitious climate policy for CH, DV5: Sufficiency of current CH climate policy "
)


#### SI Table 25 ##########################################
models_comp <- list(
  dep_102_1_int_Q84_c,
  dep_102_2_int_Q84_c,
  dep_102_3_int_Q84_c,
  dep_102_4_int_Q84_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV6", "DV7", "DV8", "DV9"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Trust in Judges",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Trust in Judges"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_25.tex",
               use.packages = F,
               caption = "DV6: CO2 Tax to reduce carbon emissions, DV7: Wind power in Mittelland, DV8: Measures to reduce meat consumption, DV9: More solar systems on open spaces "
)

#### SI Table 26 ##########################################

models_comp <- list(
  dep_intended_vote_no_int_Q84_c,
  dep_intended_vote_undecided_int_Q84_c,
  dep_intended_vote_yes_int_Q84_c,
  dep_intended_vote_2_int_Q84_c,
  dep_revealed_vote_int_Q84_c
)
texreg::texreg(lapply(models_comp, function(model) texreg::extract(model, include.ci = FALSE)),
               digits = 3, stars = c(0.001, 0.01, 0.05, 0.1),
               custom.model.names = c("DV10", "DV11", "DV12", "DV13", "DV14"),
               fontsize = "small", longtable = T, no.margin = T,
               custom.coef.names = c("Intercept",
                                     "Treatment",
                                     #Controls
                                     "Trust in Judges",
                                     "Environment",
                                     "Political Orientation",
                                     "Party: FDP",
                                     "Party: The Centre",
                                     "Party: SP",
                                     "Party: SVP",
                                     "Party: GPS",
                                     "Party: GLP",
                                     "Party: EPP",
                                     "Party: PdA",
                                     "Party: CSP",
                                     "Party: Other",
                                     "Party: Don't know/no answer",
                                     "Party: EDU",
                                     #Interaction
                                     "Treatment × Trust in Judges"),
               groups = list("Controls" = 3:17, "Interaction" = 18:18),
               custom.gof.names =c(NA, NA, "Observations", NA),
               reorder.gof =c(3, 1, 2, 4),
               file = "tables/SI_Table_26.tex",
               use.packages = F,
               caption = "DV10: Intended Vote (no/not no), DV11 Intended Vote (undecided/not undecided), DV12: Intended Vote (yes/not yes), DV13: Intended Vote (yes/no), DV14: Revealed Vote (yes/no)"
)

# Descriptive Plots ###########################################
## Wave 1 #####################################
### SI Figure 1 ################################
#age
data_age <- data_vote %>%
  mutate(age_bin = cut(age, breaks = seq(18, 90, by = 5), right = FALSE, include.lowest = TRUE)) %>%
  group_by(age_bin) %>%
  dplyr::summarise(count = n()) %>%
  mutate(percent = count / sum(count) * 100)

#summary(data$age)

#Import population statistics for age
filename <- "data/Age_CH.csv"
age_ch <- read.csv(filename, header = TRUE, sep = ";")


# Convert Percent to numeric
age_ch$Percent <- as.numeric(gsub(",", ".", age_ch$Percent))

# Create age bins for Swiss data
age_ch_bins <- age_ch %>%
  mutate(age_bin = cut(ALTER, breaks = seq(18, 90, by = 5), right = FALSE, include.lowest = TRUE)) %>%
  group_by(age_bin) %>%
  dplyr::summarise(percent = sum(Percent))

# Display Swiss data
print(age_ch_bins)

# Add a source column to distinguish between datasets
data_age <- data_age %>% mutate(source = "Sample")
age_ch_bins <- age_ch_bins %>% mutate(source = "Population")

# Combine datasets
combined_data <- bind_rows(data_age, age_ch_bins)

# Plot the data
ggplot(combined_data, aes(x = age_bin, y = percent, fill = source)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Age", y = "Percentage (%)", title = "Age Distribution Comparison") +
  scale_fill_manual(values = c("Sample" = "#4059AD", "Population" = "#CCA43B")) +
  theme_minimal() +
  theme(
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.x = element_text(size = 12, angle = 45, hjust = 1, color = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    plot.title = element_text(size = 16, face = "bold"),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA)
  )

ggsave("plots/SI_Figure_1.png", width = 6, height = 4)

### SI Figure 2 ######################
#Gender

#Import Gender Population Statistics
filename <- "data/gender_ch.csv"
gender_ch <- read.csv(filename, header = TRUE, sep = ";")

table(data_vote$gender, useNA = "ifany")
#prepare sample data
data_gender<- data_vote %>%
  filter(!is.na(gender)) %>%  # Remove NA values
  count(gender) %>% 
  filter(!gender %in% c("98", "99")) %>%
  mutate(Type = recode(gender, `0` = "male", `1` = "female")) %>%
  dplyr::select(Type, n) %>%  # 
  dplyr::rename(Count = n) %>%
  mutate(Percent = Count / sum(Count) * 100)  # Calculate percentages

data_gender <- data_gender %>% mutate(source = "Sample")
gender_ch <- gender_ch %>% mutate(source = "Population")

# Ensure Percent is numeric in both data frames
data_gender <- data_gender %>%
  mutate(Percent = as.numeric(Percent))

gender_ch <- gender_ch %>%
  mutate(Percent = gsub(",", ".", Percent)) %>% 
  mutate(Percent = as.numeric(Percent)) 


gender_ch <- gender_ch %>%
  rename(
    Type = GESCHLECHT,
    Count = Frequency
  )

combined_data_gender <- bind_rows(data_gender, gender_ch)

#create plot
ggplot(combined_data_gender, aes(x = Type, y = Percent, fill = source)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Gender", y = "Percentage (%)", title = "Gender Distribution Comparison") +
  scale_fill_manual(values = c("Sample" = "#4059AD", "Population" = "#CCA43B")) +
  theme_minimal() +
  theme(
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.x = element_text(size = 12, color = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    plot.title = element_text(size = 16, face = "bold"),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA)
  ) +
  coord_cartesian(ylim = c(45, 52)) 

ggsave("plots/SI_Figure_2.png", width = 5, height = 4)

### SI Figure 3 #######################################
# Language
#Import Gender Population Statistics
filename <- "data/Language_ch.csv"
language_ch <- read.csv(filename, header = TRUE, sep = ";")

table(data$UserLanguage)
#prepare sample data
data_language<- data_vote %>%
  filter(!is.na(UserLanguage)) %>%  # Remove NA values
  count(UserLanguage) %>%  
  filter(!UserLanguage %in% c("EN")) %>%
  mutate(Language = recode(UserLanguage, "DE" = "German", "FR" = "French")) %>%
  dplyr::select(Language, n) %>%
  rename(Frequency = n) %>%
  mutate(Percent = Frequency / sum(Frequency) * 100)  # Calculate percentages


data_language <- data_language %>% mutate(source = "Sample")
language_ch <- language_ch %>% mutate(source = "Population")

# Ensure Percent is numeric in both data frames
data_language <- data_language %>%
  mutate(Percent = as.numeric(Percent))

language_ch <- language_ch %>%
  mutate(Percent = gsub(",", ".", Percent)) %>%  # Replace commas with dots
  mutate(Percent = as.numeric(Percent))  # Convert to numeric


combined_data_language <- bind_rows(data_language, language_ch)

#create plot
ggplot(combined_data_language, aes(x = Language, y = Percent, fill = source)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Language", y = "Percentage (%)", title = "Language Distribution") +
  scale_fill_manual(values = c("Sample" = "#4059AD", "Population" = "#CCA43B")) +
  theme_minimal() +
  theme(
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.x = element_text(size = 12, color = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    plot.title = element_text(size = 16, face = "bold"),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA)
  ) +
  coord_cartesian(ylim = c(0, 85))

ggsave("plots/SI_Figure_3.png", width = 5, height = 4)

### SI Figure 4 #######################################
## Urban Rural

#Import population statistics for age
filename <- "data/urban_rural_ch.csv"
urban_rural_ch <- read.csv(filename, header = TRUE, sep = ";")


data_urban_rural <- data_vote %>%
  dplyr::filter(!is.na(urban_rural_true)) %>%  # Remove NA values
  dplyr::count(urban_rural_true) %>% 
  dplyr::mutate(Type = recode(urban_rural_true, `1` = "urban", `2` = "suburban", `3` = "rural")) %>%
  dplyr::select(Type, n) %>%  
  dplyr::rename(Count = n) %>%
  mutate(Percent = Count / sum(Count) * 100)  # Calculate percentages

data_urban_rural <- data_urban_rural %>% mutate(source = "Sample")
urban_rural_ch <- urban_rural_ch %>% mutate(source = "Population")

# Ensure Percent is numeric in both data frames
data_urban_rural <- data_urban_rural %>%
  mutate(Percent = as.numeric(Percent))

urban_rural_ch <- urban_rural_ch %>%
  mutate(Percent = gsub(",", ".", Percent)) %>%  
  mutate(Percent = as.numeric(Percent))  

urban_rural_ch <- urban_rural_ch %>%
  rename(
    Type = urbrur,
    Count = Frequency
  ) %>%
  select(Type, Count, Percent, source)

combined_data_urban_rural <- bind_rows(data_urban_rural, urban_rural_ch)

ggplot(combined_data_urban_rural, aes(x = Type, y = Percent, fill = source)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Category", y = "Percentage (%)", title = "Degree of Urbanization") +
  scale_fill_manual(values = c("Sample" = "#4059AD", "Population" = "#CCA43B")) +
  theme_minimal() +
  theme(
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.x = element_text(size = 12, color = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    plot.title = element_text(size = 14, face = "bold"),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA)
  )

ggsave("plots/SI_Figure_4.png", width = 5, height = 3)

## Wave 2 #####################################
### SI Figure 5 ################################
#age
data_age <- data_vote_fin2 %>%
  mutate(age_bin = cut(age, breaks = seq(18, 90, by = 5), right = FALSE, include.lowest = TRUE)) %>%
  group_by(age_bin) %>%
  dplyr::summarise(count = n()) %>%
  mutate(percent = count / sum(count) * 100)

#summary(data_vote_fin2$age)

#Import population statistics for age
filename <- "data/Age_CH.csv"
age_ch <- read.csv(filename, header = TRUE, sep = ";")


# Convert Percent to numeric
age_ch$Percent <- as.numeric(gsub(",", ".", age_ch$Percent))

# Create age bins for Swiss data
age_ch_bins <- age_ch %>%
  mutate(age_bin = cut(ALTER, breaks = seq(18, 90, by = 5), right = FALSE, include.lowest = TRUE)) %>%
  group_by(age_bin) %>%
  dplyr::summarise(percent = sum(Percent))

# Display Swiss data
print(age_ch_bins)

# Add a source column to distinguish between datasets
data_age <- data_age %>% mutate(source = "Sample")
age_ch_bins <- age_ch_bins %>% mutate(source = "Population")

# Combine datasets
combined_data <- bind_rows(data_age, age_ch_bins)

# Plot the data
ggplot(combined_data, aes(x = age_bin, y = percent, fill = source)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Age", y = "Percentage (%)", title = "Age Distribution Comparison") +
  scale_fill_manual(values = c("Sample" = "#4059AD", "Population" = "#CCA43B")) +
  theme_minimal() +
  theme(
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.x = element_text(size = 12, angle = 45, hjust = 1, color = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    plot.title = element_text(size = 16, face = "bold"),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA)
  )

ggsave("plots/SI_Figure_5.png", width = 6, height = 4)

### SI Figure 6 ######################
#Gender

#Import Gender Population Statistics
filename <- "data/gender_ch.csv"
gender_ch <- read.csv(filename, header = TRUE, sep = ";")

table(data_vote_fin2$gender, useNA = "ifany")
#prepare sample data
data_gender<- data_vote_fin2 %>%
  filter(!is.na(gender)) %>%  # Remove NA values
  count(gender) %>% 
  filter(!gender %in% c("98", "99")) %>%
  mutate(Type = recode(gender, `0` = "male", `1` = "female")) %>%
  dplyr::select(Type, n) %>%  # 
  dplyr::rename(Count = n) %>%
  mutate(Percent = Count / sum(Count) * 100)  # Calculate percentages

data_gender <- data_gender %>% mutate(source = "Sample")
gender_ch <- gender_ch %>% mutate(source = "Population")

# Ensure Percent is numeric in both data frames
data_gender <- data_gender %>%
  mutate(Percent = as.numeric(Percent))

gender_ch <- gender_ch %>%
  mutate(Percent = gsub(",", ".", Percent)) %>% 
  mutate(Percent = as.numeric(Percent)) 


gender_ch <- gender_ch %>%
  rename(
    Type = GESCHLECHT,
    Count = Frequency
  )

combined_data_gender <- bind_rows(data_gender, gender_ch)

#create plot
ggplot(combined_data_gender, aes(x = Type, y = Percent, fill = source)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Gender", y = "Percentage (%)", title = "Gender Distribution Comparison") +
  scale_fill_manual(values = c("Sample" = "#4059AD", "Population" = "#CCA43B")) +
  theme_minimal() +
  theme(
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.x = element_text(size = 12, color = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    plot.title = element_text(size = 16, face = "bold"),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA)
  ) +
  coord_cartesian(ylim = c(45, 52)) 

ggsave("plots/SI_Figure_6.png", width = 5, height = 4)

### SI Figure 7 #######################################
# Language
#Import Gender Population Statistics
filename <- "data/Language_ch.csv"
language_ch <- read.csv(filename, header = TRUE, sep = ";")

table(data$UserLanguage)
#prepare sample data
data_language<- data_vote_fin2 %>%
  filter(!is.na(UserLanguage)) %>%  # Remove NA values
  count(UserLanguage) %>%  
  filter(!UserLanguage %in% c("EN")) %>%
  mutate(Language = recode(UserLanguage, "DE" = "German", "FR" = "French")) %>%
  dplyr::select(Language, n) %>%
  rename(Frequency = n) %>%
  mutate(Percent = Frequency / sum(Frequency) * 100)  # Calculate percentages


data_language <- data_language %>% mutate(source = "Sample")
language_ch <- language_ch %>% mutate(source = "Population")

# Ensure Percent is numeric in both data frames
data_language <- data_language %>%
  mutate(Percent = as.numeric(Percent))

language_ch <- language_ch %>%
  mutate(Percent = gsub(",", ".", Percent)) %>%  # Replace commas with dots
  mutate(Percent = as.numeric(Percent))  # Convert to numeric


combined_data_language <- bind_rows(data_language, language_ch)

#create plot
ggplot(combined_data_language, aes(x = Language, y = Percent, fill = source)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Language", y = "Percentage (%)", title = "Language Distribution") +
  scale_fill_manual(values = c("Sample" = "#4059AD", "Population" = "#CCA43B")) +
  theme_minimal() +
  theme(
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.x = element_text(size = 12, color = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    plot.title = element_text(size = 16, face = "bold"),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA)
  ) +
  coord_cartesian(ylim = c(0, 85))

ggsave("plots/SI_Figure_7.png", width = 5, height = 4)

### SI Figure 8 #######################################
## Urban Rural

#Import population statistics for age
filename <- "data/urban_rural_ch.csv"
urban_rural_ch <- read.csv(filename, header = TRUE, sep = ";")


data_urban_rural <- data_vote_fin2 %>%
  dplyr::filter(!is.na(urban_rural_true)) %>%  # Remove NA values
  dplyr::count(urban_rural_true) %>% 
  dplyr::mutate(Type = recode(urban_rural_true, `1` = "urban", `2` = "suburban", `3` = "rural")) %>%
  dplyr::select(Type, n) %>%  
  dplyr::rename(Count = n) %>%
  mutate(Percent = Count / sum(Count) * 100)  # Calculate percentages

data_urban_rural <- data_urban_rural %>% mutate(source = "Sample")
urban_rural_ch <- urban_rural_ch %>% mutate(source = "Population")

# Ensure Percent is numeric in both data frames
data_urban_rural <- data_urban_rural %>%
  mutate(Percent = as.numeric(Percent))

urban_rural_ch <- urban_rural_ch %>%
  mutate(Percent = gsub(",", ".", Percent)) %>%  
  mutate(Percent = as.numeric(Percent))  

urban_rural_ch <- urban_rural_ch %>%
  rename(
    Type = urbrur,
    Count = Frequency
  ) %>%
  select(Type, Count, Percent, source)

combined_data_urban_rural <- bind_rows(data_urban_rural, urban_rural_ch)

ggplot(combined_data_urban_rural, aes(x = Type, y = Percent, fill = source)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Category", y = "Percentage (%)", title = "Degree of Urbanization") +
  scale_fill_manual(values = c("Sample" = "#4059AD", "Population" = "#CCA43B")) +
  theme_minimal() +
  theme(
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.x = element_text(size = 12, color = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    plot.title = element_text(size = 14, face = "bold"),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA)
  )

ggsave("plots/SI_Figure_8.png", width = 5, height = 3)
